using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: HN X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class SOIKEO
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();
		public SOIKEO(SMS_MO MO)
		{
			WMO = MO;
		}

		public SOIKEO()
		{
		}
		#endregion
		public void run()
		{
			WMT = new SMS_MT(WMO);
			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
			string[] msg = tmpMsg.Split(new char[] { ' ' });

			string strReturnMsg = "";
			if((WMO.ServiceID.Replace("04","")=="6772") || ((WMO.CommandCode.ToUpper()=="SS" || WMO.CommandCode.ToUpper()=="AM" || WMO.CommandCode.ToUpper()=="AT") && (WMO.ServiceID.Replace("04","")=="6572")))
			{
				strReturnMsg = GetReturnMsg(msg);
			}
			else
			{
				strReturnMsg ="Cam on ban!Voi cac cp TIP,TIP2,TIP3 chung toi chi ap dung dau so 6772,cp SS,AM,AT ap dung cho dau so 6572 va 6772";
			}
			if(strReturnMsg.Length>160)
			{
				string supertemp="";
				supertemp = strReturnMsg;
				strReturnMsg = strReturnMsg.Substring(0,160);

				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				strReturnMsg = supertemp.Substring(160);

				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				//
				WMT.Insert();
			}
			else
			{
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
			return;  
		}
		public string GetReturnMsg(string[] arr)
		{
			string strSQL="";
			string msgBody="";
			string d = WMO.CommandCode.ToUpper();
			string connStr= ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
			SqlConnection sqlconn = new SqlConnection(connStr);
			sqlconn.Open();
			strSQL = "SELECT * FROM SOIKEO WHERE Code='" + d + "'";
			SqlDataAdapter da = new SqlDataAdapter(strSQL, sqlconn);
			DataSet ds = new DataSet();
			da.Fill(ds);
			sqlconn.Close();
			msgBody = ds.Tables[0].Rows[0]["MsgReturn"].ToString();
			return msgBody;
		}
	}		
}
